package com.gt.mapper;

import com.github.pagehelper.Page;
import com.gt.pojo.dto.BorrowPageQueryDTO;
import com.gt.pojo.entity.Borrow;
import com.gt.pojo.vo.BorrowVO;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Mapper
public interface BorrowMapper {
    Page<BorrowVO> pageQuery(BorrowPageQueryDTO borrowPageQueryDTO);

    void update(Borrow borrow);

    @Select("select *from library.borrow where id = #{id}")
    Borrow selectById(Long id);


    @Insert("insert into library.borrow(user_id, start_time, end_time, status, book_id, name) " +
            "VALUES (#{userId},#{startTime},#{endTime},#{status},#{bookId},#{name})")
    void insert(Borrow borrow);

    Page<BorrowVO> query(BorrowPageQueryDTO borrowPageQueryDTO);

    @Select("select *from library.borrow where user_id = #{userId} and book_id = #{bookId}")
    List<Borrow> selectByBookId(@Param("bookId")Long bookId, @Param("userId") Long userId);

    @Select("select *from library.borrow where book_id = #{booId}")
    List<Borrow> selectByBoId(Long booId);

    @Select("select *from library.borrow where status = #{status}  and end_time < #{endTime}")
    List<Borrow> getByStatusAndEndTimeLT( @Param("status") Integer status,@Param("endTime") LocalDateTime endTime);


    @Select("select count(id) from library.borrow where status != #{status} ")
    Integer getByStatus(@Param("status") Integer status);

    @Select("select *from library.borrow where user_id = #{userId}")
    List<Borrow> selectByUserId(@Param("userId") Long userId);

    @Delete("delete from library.borrow where id = #{id}")
    void deleteById(Long id);
}
